{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-14T05:04:20.404947Z",
     "start_time": "2025-01-14T05:04:19.563839Z"
    }
   },
   "source": [
    "import pymongo\n",
    "\n",
    "client = pymongo.MongoClient(host='139.196.92.103', port=27017, username='root', password='wby6225104')\n",
    "col = client['jx3_pintuan']['mongo_learn']"
   ],
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "raw",
   "source": [
    "# 数据库\n",
    "```\n",
    "show dbs  ##显示所有数据库\n",
    "use jx3_pintuan  ##切换数据库\n",
    "db.col_name.drop ## 删除\n",
    "```\n",
    "# 集合\n",
    "```\n",
    "db.createCollection('mongo_learn')  ##创建集合: 显示创建的集合\n",
    "db.getCollectionNames()  ##显示所有的集合\n",
    "db.dropCollection('mongo_learn')  ##删除集合\n",
    "```"
   ],
   "id": "d442c532c81c8211"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-14T05:04:22.269504Z",
     "start_time": "2025-01-14T05:04:22.180964Z"
    }
   },
   "cell_type": "code",
   "source": [
    "## 插入单条数据\n",
    "col.insert_one({'_id': 1, 'name': 'wby1'})\n",
    "## 插入多条\n",
    "col.insert_many([{'_id': 2, 'name': 'wby2'}, {'_id': 3, 'name': 'wby3'}])\n",
    "\n",
    "for i in col.find():\n",
    "    print(i)"
   ],
   "id": "db6f9629e75eb54e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_id': 1, 'name': 'wby'}\n",
      "{'_id': 2, 'name': 'wby2'}\n",
      "{'_id': 3, 'name': 'wby3'}\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 查询\n",
    "\n",
    "## 投影查询\n",
    "col.find({\n",
    "    \"_id\":\"2323\",\n",
    "})\n"
   ],
   "id": "2f7018bd9ee22bba"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
